import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        TreeMap<Object, Object> treeMap = new TreeMap<>();
        treeMap.put(0, -1);
        treeMap.put(1, 1);
        treeMap.put(2, 2);
        System.out.println(treeMap);

        treeMap.put(1, 11);
        System.out.println(treeMap);

        treeMap.putIfAbsent(1, "1");
        treeMap.putIfAbsent(11, "11");
        System.out.println(treeMap);

        treeMap.compute(0, (k, v) -> {
            if (v instanceof Integer tmp) {
                return tmp == 0 ? 0 : tmp < 0 ? ++tmp : --tmp;
            } else {
                return "not integer";
            }
        });

        System.out.println(treeMap);
        System.out.println("-----------------");

        treeMap.computeIfAbsent(0, String::valueOf);
        treeMap.computeIfAbsent(22, String::valueOf);
        System.out.println(treeMap);
        System.out.println("-----------------");

        treeMap.computeIfPresent(0, List::of);
        treeMap.computeIfPresent(33, Set::of);
        System.out.println(treeMap);
        System.out.println("-----------------");

        treeMap.merge(0,111,(val,addVal)->List.of(((List) val).stream().collect(Collectors.toList()), addVal));
        System.out.println(treeMap);
        System.out.println("-----------------");

        treeMap.merge(0,222, (val,addVal)->{
            Set collect = (Set) ((List) val).stream().collect(Collectors.toSet());
            collect.add(addVal);
            return collect;
        });
        System.out.println(treeMap);

    }
}
